<?php
require_once(dirname(__FILE__)."/inc/debug.php");
require_once(dirname(__FILE__)."/inc/class/common/lang/LangFileManager.php");
require_once(dirname(__FILE__)."/inc/response.php");

$strAct = $_POST['act'];

if( !isset($strAct) ) {
	make_error_response( WF_ERRCODE_NOACT, WF_ERRCODE_NOACT_MSG);
	exit;
}
switch($strAct) {
	case "getLang":
        $objLangFileManager = WF::getLangFileFactory()->getLangFileManager();
        $strResult = $objLangFileManager->getLangFromMapping($_POST['lang']);
        echo $strResult;
		break;
    case "getLangFile":
		mydebug_log("[controller] act:".$strAct.", lang:".$_POST['lang'].", folder:".$_POST['folder'].", langType:".$_POST['langType'].", userGenerated:".$_POST['userGenerated']);
		// check input
		if( (!isset($_POST['lang']))|| !isset($_POST['folder']) || !isset($_POST['langType']) || !isset($_POST['userGenerated'])) {
			make_error_response( WF_ERRCODE_LACKINPUT, WF_ERRCODE_LACKINPUT_MSG);
			exit;
		}
		WF::getIOFactory()->getFile()->setXMLHeader();
		$objLangFileManager = WF::getLangFileFactory()->getLangFileManager();
		try{
			$strResult = $objLangFileManager->getLangFile($_POST['lang'], $_POST['folder'], $_POST['langType'], $_POST['userGenerated']);
			echo $strResult;
		}catch(Exception $e){
			mydebug_log("[controller] act:".$strAct."-".$e->getMessage());
			make_error_response( WPM_ERRCODE_LANG_XML_LOAD_FAIL, WPM_ERRCODE_LANG_XML_LOAD_FAIL_MSG."(act=getLangFile): ".$e->getMessage());
			exit;
		}
		break;
	case "getJsonLangFileByCsvFolder":
		    /**
		     * lang:en_US
		     * folder:modActiveUser,modActiveUserWR,modDLPTemplateMatch,modDLPTopViolationPolicy,modDLPTopViolators,modDLPViolationCnt,modEndpointEncryptionStatus,modFDE,modHttpTraffic,modHttpTrafficWR,modPolicyResultStatus,modQuickLaunch,modRealTimeSystemStatus,modTopInfectionClient,modTopInfectionClientWR,modDSComponentSummary,modDSFeatureSummary,modDSStatusSummary,modTmcmAD,modTmcmActiveRoamingClient,modTmcmAppCompliance,modTmcmAppStatusClient,modTmcmAppStatusSrv,modTmcmCompCompliance,modTmcmERSSpamSource,modTmcmFRSDetection,modTmcmFRSTopDetection,modTmcmSPNCompliance,modTmcmSPNThreatStatistics,modTmcmThreatDetection,modTmcmThreatStatistics,modTmcmThreatsDistributed,modTmcmTopThreats,modTmcmViolationStatus,modTmcmWRSBadActor,modTmcmWRSTarVictim
		     * langType:WIDGET_LANG
		     * userGenerated:0
		     */
			mydebug_log("[controller] act:".$strAct.", lang:".$_POST['lang'].", folder:".$_POST['folder'].", langType:".$_POST['langType'].", userGenerated:".$_POST['userGenerated']);
			// check input
			if( (!isset($_POST['lang']))|| !isset($_POST['folder']) || !isset($_POST['langType']) || !isset($_POST['userGenerated'])) {
				make_error_response( WF_ERRCODE_LACKINPUT, WF_ERRCODE_LACKINPUT_MSG);
				exit;
			}
		
			$objLangFileManager = WF::getLangFileFactory()->getLangFileManager();
			try{
				$arrFolder = (isset($_POST['folder']) && $_POST['folder']) ? explode(',',$_POST['folder']) : array();
				/*
				 $arrRet = array (
				 		'modX' =>
				 		array (
				 				'title' => 'Create Policy',
				 				'general_infomation' => 'General Information',
				 				'name' => 'Name: ',
				 		),
				 		'modY' =>
				 		array (
				 				'title' => 'Create Policy',
				 				'general_infomation' => 'General Information',
				 				'name' => 'Name: ',
				 		),
				 )
				*/
				$arrRet = array();
				foreach($arrFolder as $strFolder){
					$strXml = $objLangFileManager->getLangFile($_POST['lang'], $strFolder, $_POST['langType'], $_POST['userGenerated']);
					$elTranslations = new SimpleXMLElement($strXml, null);
					$arrElTranslationsString = $elTranslations->xpath('/translations/string');
					if(is_array($arrElTranslationsString) && $arrElTranslationsString){
						foreach($arrElTranslationsString as $elTranslationsString){
							$id = (string)$elTranslationsString->attributes()->id;
							$val = (string)$elTranslationsString;
							$arrRet[$strFolder][$id] = $val;
						}
					}
				}
				//{
				//"modX":{"title":"Create Policy","general_infomation":"General Information","name":"Name: "},
				//"modY":{"title":"Create Policy","general_infomation":"General Information","name":"Name: "}
				//}
				//$strResult = $objLangFileManager->getLangFile($_POST['lang'], $_POST['folder'], $_POST['langType'], $_POST['userGenerated']);
				echo json_encode($arrRet);
			}catch(Exception $e){
				mydebug_log("[controller] act:".$strAct."-".$e->getMessage());
				make_error_response( WPM_ERRCODE_LANG_XML_LOAD_FAIL, WPM_ERRCODE_LANG_XML_LOAD_FAIL_MSG."(act=getLangFile): ".$e->getMessage());
				exit;
			}
			break;
	case "writeClientLog":
		$level = $_POST['level'];
		$message = $_POST['message'];

		mydebug_log("[controller] act:".$strAct.", level:".$level.", message:".$message);

		if(!class_exists('WF')){
		    require_once(dirname(__FILE__).'/inc/class/init.php');
		}
		WF::getLoggerClient()->logInfo($message,$level);
		break;
	case "getCurrentTurnedOnCategoryInfo":
		mydebug_log("[WFTALK] act:".$_POST['act']);

		$strCategoryName = isset($_POST['category_name']) ? $_POST['category_name'] : '';
		$response = WF::getWidgetPoolFactory()->getWidgetPoolDB()->getAssocCurrentTurnedOnCategoryInfo($strCategoryName);
		$response = json_encode($response);
		
		print $response;
		break;
	default:
		mydebug_log("[case=>default::controller] act:".$_POST['act']);
		make_error_response( WF_ERRCODE_ACTERR, WF_ERRCODE_ACTERR_MSG);
		exit;
}
